#include "ResultLogger.h"
#include <QCoreApplication>
#include <QFile>
#include <QFileInfo>
#include "Msg.h"
#include <QDir>
#include <QDataStream>
#include <QApplication>
#include <QDesktopServices>
#include <QTableView>
#include <QStandardItemModel>
#include "../Settings/GameSettings.h"
#include <QVBoxLayout>
#include <QHeaderView>
#include <QStandardItem>
#include <QDialog>

const QString ResultLogger::LogFileName = QString("reslog");
ResultLogger *ResultLogger::mpMe = NULL;

ResultLogger::ResultLogger(QObject *parent) :
    QObject(parent)
{
    connect(QApplication::instance(),SIGNAL(lastWindowClosed()),SLOT(save()));
    restore();
}

ResultLogger *ResultLogger::me()
{
    if(mpMe == NULL){
        mpMe = new ResultLogger();
    }

    return mpMe;
}

void ResultLogger::restore()
{
    mResults.clear();
    QString path = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
    QFileInfo fi(path + QString(QDir::separator()) + LogFileName);

    if(fi.exists()){
        if(!fi.isReadable()){
            Msg::err("File, " + fi.filePath() + "is not readable.","This file should have permission to view 'Results Log'.");
            return;
        }

        QFile file(fi.filePath ());
        file.open(QFile::ReadWrite);
        QDataStream stream(&file);
        int nCount = 0;
        stream >> nCount;

        for(int i  = 0; i < nCount; i++){
            QStringList row;

            for(int j = 0; j < MaxCol; j++){
                QString str;
                stream >> str;
                row << str;
            }

            mResults.append(row);
        }

        file.close();
    }
}

void ResultLogger::save()
{
    QString path = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
    QFileInfo fi(path + QString(QDir::separator()) + LogFileName);

    QFile file(fi.filePath ());
    file.open(QFile::ReadWrite);
    QDataStream stream(&file);
    int nCount = mResults.count();
    stream << nCount;

    for(int i  = 0; i < nCount; i++){
        QStringList row = mResults.at(i);

        for(int j = 0; j < MaxCol; j++){
            QString str = row.at(j);
            stream << str;
        }
    }

    file.close();

}

void ResultLogger::addAResult(QStringList item)
{
    mResults.append(item);
}

void ResultLogger::populateResult(QStandardItemModel *pModel)
{
    int nCount = mResults.count() - 1;

    for(int i = nCount; i >= 0; i--){
        populateARow(pModel,mResults.at(i), nCount - i);
    }
}

void ResultLogger::populateARow(QStandardItemModel *pModel, QStringList value, int nRow)
{
    for(int i = 0; i < value.count(); i++){
        pModel->setData(pModel->index(nRow,i,QModelIndex()),value.at(i),Qt::EditRole);
        QStandardItem *pItem = pModel->item(nRow,i);

        if(pItem){
            pItem->setEditable(false);
        }
    }
}

void ResultLogger::showLog()
{
    QStandardItemModel *pModel = new QStandardItemModel(mResults.count(),MaxCol);
    QStringList lbsHorz;
    lbsHorz << "Winner"<< "Looser" << "Mode" << "Result" <<"Date";
    pModel->setHorizontalHeaderLabels(lbsHorz);
    QItemSelectionModel *pSelectionModel = new QItemSelectionModel(pModel);
    populateResult(pModel);
    QTableView *pTableView = new QTableView();
    pTableView->setModel(pModel);
    pTableView->setSelectionModel(pSelectionModel);
    pTableView->horizontalHeader()->setStretchLastSection(true);
    QDialog *widegt = new QDialog(GameSettings::me()->mostTopWnd());
    widegt->setWindowTitle("Dot Game Result Log");
    QVBoxLayout *pLayout = new QVBoxLayout();
    pLayout->addWidget(pTableView);
    widegt->setLayout(pLayout);
    widegt->setWindowModality(Qt::WindowModal);
    widegt->resize(550,400);
    widegt->show();
}
